<script>
    class Promise {
        // 构造函数接收一个executor函数
        constructor(executor) {
            // 初始化promise状态为pending
            this._status = 'pending';
            // 初始化promise的值为undefined
            this._value = undefined;
            // 初始化promise的回调函数
            this._callbacks = [];
            // 定义resolve函数，用于将promise状态修改为fulfilled
            const resolve = (value) => {
                // 只有在promise状态为pending时才能修改状态
                if (this._status === 'pending') {
                    // 修改promise状态为fulfilled
                    this._status = 'fulfilled';
                    // 修改promise的值为value
                    this._value = value;
                    // 调用所有的回调函数
                    this._callbacks.forEach((callback) => {
                        // console.log(callback)
                        callback.onFulfilled(value);
                    });
                }
            };

            // 定义reject函数，用于将promise状态修改为rejected
            const reject = (reason) => {
                // 只有在promise状态为pending时才能修改状态
                if (this._status === 'pending') {
                    // 修改promise状态为rejected
                    this._status = 'rejected';
                    // 修改promise的值为reason
                    this._value = reason;
                    // 调用所有的回调函数
                    this._callbacks.forEach((callback) => {
                        callback.onRejected(reason);
                    });
                }
            };

            // 执行executor函数，并传入resolve和reject函数作为参数
            executor(resolve, reject);
        }

        // then方法用于添加成功和失败的回调函数
        then(onFulfilled, onRejected) {
            // 如果onFulfilled和onRejected不是函数，则忽略它们
            onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : (value) => value;
            onRejected = typeof onRejected === 'function' ? onRejected : (reason) => { throw reason };

            // 创建一个新的promise对象
            const promise2 = new Promise((resolve, reject) => {
                if (this._status === 'fulfilled') {
                    // 如果当前promise状态为fulfilled，则立即执行onFulfilled回调函数
                    // 并将其返回值作为promise2的值
                    setTimeout(() => {
                        try {
                            const x = onFulfilled(this._value);

                            resolvePromise(promise2, x, resolve, reject);
                        } catch (error) {
                            reject(error);
                        }
                    });
                } else if (this._status === 'rejected') {
                    // 如果当前promise状态为rejected，则立即执行onRejected回调函数
                    // 并将其返回值作为promise2的值
                    setTimeout(() => {
                        try {
                            const x = onRejected(this._value);
                            resolvePromise(promise2, x, resolve, reject);
                        } catch (error) {
                            reject(error);
                        }
                    });
                } else {
                    // 如果当前promise状态为pending，则将回调函数添加到_callbacks数组中
                    // 在promise状态变为fulfilled或rejected时执行回调函数
                    this._callbacks.push({
                        onFulfilled: (value) => {
                            setTimeout(() => {
                                try {
                                    const x = onFulfilled(value);

                                    resolvePromise(promise2, x, resolve, reject);
                                } catch (error) {
                                    reject(error);
                                }
                            });
                        },
                        onRejected: (reason) => {
                            setTimeout(() => {
                                try {
                                    const x = onRejected(reason);
                                    resolvePromise(promise2, x, resolve, reject);
                                } catch (error) {
                                    reject(error);
                                }
                            });
                        },
                    });
                }
            });

            // 返回新的promise对象
            return promise2;
        }
    }

    // 定义一个resolvePromise函数，用于解析then方法中的返回值，并传递给新的promise对象
    function resolvePromise(promise, x, resolve, reject) {

        // 如果promise和x引用同一个对象，则抛出TypeError异常
        if (promise === x) {
            reject(new TypeError('Chaining cycle detected for promise'));
        }

        // 如果x是一个promise对象，则等待该promise对象的状态改变后，再传递给新的promise对象
        if (x instanceof Promise) {
            x.then(
                (value) => {
                    resolvePromise(promise, value, resolve, reject);
                },
                (reason) => {
                    reject(reason);
                }
            );
        } else {
            // 如果x是一个普通值，则直接将其传递给新的promise对象
            resolve(x);
        }
    }

    // 测试代码
    const p = new Promise((resolve, reject) => {

        resolve('success');

    })

    let p2 = p.then(a => p)
  



</script>